package com.cooper.controllers.general;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.cooper.constants.AppConstants;
import com.cooper.constants.DictConstants;
import com.cooper.constants.ResourceConstants;
import com.cooper.controllers.Controller;
import com.cooper.controllers.LoginRequired;
import com.cooper.core.JsonResult;
import com.cooper.entity.AdviceReplyEntity;
import com.cooper.entity.AdviceSubjectEntity;
import com.cooper.entity.DesignerEntity;
import com.cooper.entity.UserInfo;
import com.cooper.pagination.PageInfo;
import com.cooper.pagination.PageResult;
import com.cooper.service.core.DictService;
import com.cooper.service.core.UserService;
import com.cooper.service.general.AdviceService;
import com.cooper.service.seller.DesignerService;
import com.cooper.util.Controllers;
import com.cooper.vo.AdviceSearchVo;
import com.cooper.vo.AdviceSubjectVo;
import com.google.common.collect.Lists;

import net.paoding.rose.web.Invocation;
import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;

@LoginRequired
public class AdviceController extends Controller {
	
	@Autowired
	private DictService dictService;
	
	@Autowired
	private AdviceService adviceService;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private DesignerService designerService;
	
	@Get @Post
	public String execute(Invocation inv, AdviceSearchVo searchVo, PageInfo pageInfo) throws Exception{
		inv.addModel("adviceSubjectTypeMap", this.dictService.makeOptionList(DictConstants.ADVICE_SUBJECT_TYPE, null, null));
		pageInfo.setPageSize(6);
		PageResult<AdviceSubjectEntity> pageResult = this.adviceService.queryForList(searchVo, pageInfo);
		List<AdviceSubjectVo> subjectList = Lists.newArrayList();
		if(CollectionUtils.isNotEmpty(pageResult.getResult())){
			for(AdviceSubjectEntity entity: pageResult.getResult()){
				AdviceSubjectVo subjectVo = new AdviceSubjectVo();
				BeanUtils.copyProperties(entity, subjectVo);
				UserInfo userInfo = this.userService.getUser(entity.getCreator());
				if(null != userInfo){
					subjectVo.setCreatorHeader(userInfo.getUserFileId());
				}
				subjectVo.setReplyList(this.adviceService.getReplyList(entity.getId()));
				subjectList.add(subjectVo);
			}
		}
		inv.addModel("entryList", subjectList);
		inv.addModel(AppConstants.pageInfo, pageInfo);
		inv.addModel("searchVo", searchVo);
		return "AdviceMain";
	}
	
	@Get("newSubject")
	public String newSubject(Invocation inv){
		inv.addModel("adviceSubjectTypeMap", this.dictService.makeOptionList(DictConstants.ADVICE_SUBJECT_TYPE, null, null));
		return "AdviceSubject";
	}
	
	@Post("saveSubject")
	public Object saveSubject(Invocation inv, AdviceSubjectEntity subject) throws Exception {
		JsonResult json = new JsonResult();
		UserInfo userInfo = Controllers.getUserInfo(inv);
		try{
			DesignerEntity designer = this.designerService.findDesignerByUserId(userInfo.getId());
			subject.setUserType(null == designer?ResourceConstants.UserType.BUYER: ResourceConstants.UserType.SELLER);
			Controllers.prepareEntityForDataSaving(subject, userInfo, false);
			this.adviceService.saveSubject(subject);
			json.setSuccess(true);
		} catch(Exception e){
			json.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return json;
	}
	
	@Get("newReply")
	public String newReply(Invocation inv, @Param("subjectId") long subjectId) throws Exception {
		inv.addModel("subjectId", subjectId);
		return "AdviceReply";
	}
	
	@Post("saveReply")
	public Object saveReply(Invocation inv, AdviceReplyEntity reply) throws Exception {
		JsonResult json = new JsonResult();
		UserInfo userInfo = Controllers.getUserInfo(inv);
		try{
			Controllers.prepareEntityForDataSaving(reply, userInfo, false);
			this.adviceService.saveReply(reply);
			json.setSuccess(true);
		} catch(Exception e){
			json.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return json;
	}

}
